package com.woniuxy.woniubookmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.woniubookmanager.entity.Manager;
import com.woniuxy.woniubookmanager.mapper.ManagerMapper;
import com.woniuxy.woniubookmanager.service.ManagerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.woniubookmanager.utils.JwtUtils;
import com.woniuxy.woniubookmanager.vo.ManagerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 内部员工信息 服务实现类
 * </p>
 *
 * @author wugd
 * @since 2022-11-29 04:03:13
 */
@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {

    @Autowired
    ManagerMapper managerMapper ;

    @Override
    public Manager getManagerByName(String username) {
        try {
            QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_name",username) ;
            Manager manager = this.getOne(queryWrapper);
            return manager ;
        }catch (RuntimeException e){
            throw new RuntimeException("系统错误!");
        }
    }

    @Override
    public String getAccount(String token) {
        String account = JwtUtils.getAccount(token);
        return account;
    }

    @Override
    public Page<Manager> getManagerByPage(Integer now, Integer size, String mname) {
        Page<Manager> page = new Page<>(now,size);
        QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        if (mname != null && !"".equals(mname.trim())){
            queryWrapper.like("user_name",mname.trim());
        }
        Page<Manager> managerPage = managerMapper.selectPage(page, queryWrapper);
        return managerPage;
    }

    @Override
    @Transactional
    public void modifyManager(ManagerVo managerVo) {
        //1. 修改管理员
        managerMapper.update(managerVo);
        //2. 删除角色 关旭
        managerMapper.deleteManagerAndRoleOfRelationByMid(managerVo.getId());
        //3 .保存新的角色关系
        managerMapper.saveManagerAndRoleOfRelationByMid(managerVo);
    }
}
